package 语法.解析数据

import groovy.xml.MarkupBuilder
//java处理xml有sax和dom两种方式
//如何解析XMl
//不用得到具体的实体，可以直接调用获取属性
def String xml = '''
        <response version="1.0">
            <notes>
                <note id = 'iidd1'>
                    <to>Tove</to>
                    <from>Jani1</from> 
                </note>
                <note id = 'iidd2'>
                    <to>Tove1</to>
                    <from>Jani2</from> 
                </note>
            </notes>
            <notes>
                <note id = 'iidd3'>
                    <to>Tove2</to>
                    <from>Jani3</from> 
                </note>
                <note id = 'iidd4'>
                    <to>Tove13</to>
                    <from>Jani4</from> 
                </note>
            </notes>
            <notes>
                <note id = 'iidd5'>
                    <to>Tove</to>
                    <from>Jani5</from> 
                </note>
                <note id = 'iidd6'>
                    <to>Tove</to>
                    <from>Jani6</from> 
                </note>
            </notes>
        </response>
    '''
def xmlSluper = new XmlSlurper()
def response = xmlSluper.parseText(xml)
//访问获取节点的值,
//表示第一个notes下的第一个note的标签为to的标签内容
//不用得到具体的实体，可以直接调用获取属性
println response.notes[0].note[0].to.text()
//访问获取节点的属性
//表示第一个notes下的第一个note的id属性值
println response.notes[0].note[0].@id

//普通遍历
def list = [];
response.notes.each {
    notes ->
        //notes节点下面都是note节点
        notes.note.each {
            note ->
                def to = note.to.text()
                if (to.equals("Tove")) {
                    list.add(note.from.text());
                }
        }
}
println list

//递归的遍历，深度遍历
def tos = response.depthFirst().findAll {
    note -> note.to.text() == 'Tove'
}
println tos

//广度遍历
def name = response.notes.children().findAll {
    note -> note.@id == 'iidd6'
}
println name;
println name.class;
def from = name.collect {
    note -> return note.from.text()
}
println from


//创建生成xml
/**
 * <langs type='current' count='3' mainstream='true'>
 *   <language flavor='static' version='1.5'>java
 *     <age>16</age>
 *     <aa>qwe</aa>
 *   </language>
 *   <language flavor='dynamic' version='1.6'>jjaa
 *     <age>10</age>
 *   </language>
 *   <language flavor='dynamic' version='1.9'>JavaScript</language>
 * </langs>
 */
def sw = new StringWriter()
def xmlBuilder = new MarkupBuilder(sw) //用来生成xml数据的核心类
//根结点langs创建成功，指定langs的属性值
//langs和language看起来像一个方法，但是不是方法，其实是标签的名字
xmlBuilder.langs(type: 'current', count: '3', mainstream: 'true') {
    //第一个language结点，指明属性flavor，version，没有指定属性的就是标签的值标签的值
    //age就是language节点的子节点
    language(flavor: 'static', version: '1.5', 'java') {
        age('16')
        aa('qwe')
    }
    language(flavor: 'dynamic', version: '1.6','jjaa') {
        age('10')
    }
    language(flavor: 'dynamic', version: '1.9', 'JavaScript')
}
println sw
println "---------ssss--------------------------------------------------------------------"


//根据实体类动态的生成xml
/**
 * <langs type='current' count='3' mainstream='true'>
 *   <language flavor='static' version='1.5'>java</language>
 *   <language flavor='dymic' version='1.6'>js</language>
 *   <language flavor='static' version='1.7'>jj</language>
 * </langs>
 */
def sw1 = new StringWriter()
def xmlBuilder1 = new MarkupBuilder(sw1)
def langs = new Langs();
xmlBuilder1.langs(type:langs.type,count:langs.count,mainstream:langs.mainstream){
    langs.language.each {
        //不带key的就是给节点赋值
        languages -> language(flavor: languages.flavor,version: languages.version,languages.value)
    }
}
println sw1
